<?php

namespace app\model\system;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 模型-order
 * Class OrderModel
 * @package app\model
 */
class LogisticsModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'logistics';

    public function __construct(array $data = [])
    {
        parent::__construct($data);
    }

    /**
     * 店铺id 查询范围
     * @param $query
     * @param $id
     */
    public function scopeStoreId($query, $id)
    {
        if (!empty($id)) {
            $query->alias("a");
            $query->where('a.store_id', $id);
        }
    }

    /**
     * 范围查询-keyword
     * @param $query
     * @param $keyword
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('logistics_name', '%' . trim($keyword) . '%');
        }
    }

    /**
     * 获取分页列表
     * @param $keyword
     * @param $current_page
     * @param $list_rows
     * @param string $field
     * @param string $store_id
     * @return array
     */
    public function getPageData($keyword, $current_page, $list_rows, $field = '*',$store_id='')
    {
        $dataList = $this
            ->field($field)
            ->storeId($store_id)
            ->keyword($keyword)
            ->order('create_time', 'desc')
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);

        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $dataList->items()];
        return $ret;
    }

    /**
     * @param $code
     * @return mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getLogisticsName($code)
    {
        $map = ['logistics_code' => $code];
        $logistics = self::where($map)->field('logistics_name')->find();
        if (empty($logistics)) {
            return "";
        }
        return $logistics['logistics_name'];
    }
}
